package com.xidian.zhiancloud.service.ledgerManager.targerAndSystem;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.SecurityWorkPerformanceLedgerMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.SecurityWorkPerformanceLedger;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.ledgerManagerment.targetAndSystem.SecurityWorkPerformanceLedgerQO;
import com.xidian.zhiancloud.vo.ledgerManagerment.targetAndSystem.SecurityWorkPerformanceLedgerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Summerless
 * @Date: 2020/11/8 23:22
 */
@Service
public class SecurityWorkPerformanceLedgerService extends ServiceImpl<SecurityWorkPerformanceLedgerMapper, SecurityWorkPerformanceLedger> implements IService<SecurityWorkPerformanceLedger> {
    @Autowired
    private SecurityWorkPerformanceLedgerMapper securityWorkPerformanceLedgerMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    public IPage<SecurityWorkPerformanceLedgerVO> selectByQO(SecurityWorkPerformanceLedgerQO securityWorkPerformanceLedgerQO) {
        Page<SecurityWorkPerformanceLedger> page = new Page<>(securityWorkPerformanceLedgerQO.getCurrent(), securityWorkPerformanceLedgerQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        LambdaQueryWrapper<SecurityWorkPerformanceLedger> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(securityWorkPerformanceLedgerQO.getAssessedOrganizationId() != null, SecurityWorkPerformanceLedger::getAssessedOrganizationId, securityWorkPerformanceLedgerQO.getAssessedOrganizationId())
                .between(securityWorkPerformanceLedgerQO.getStartTime() != null && securityWorkPerformanceLedgerQO.getEndTime() != null, SecurityWorkPerformanceLedger::getCreateTime, securityWorkPerformanceLedgerQO.getStartTime(), securityWorkPerformanceLedgerQO.getEndTime())
                .eq(securityWorkPerformanceLedgerQO.getCompanyId() != null, SecurityWorkPerformanceLedger::getCompanyId, securityWorkPerformanceLedgerQO.getCompanyId());
        IPage<SecurityWorkPerformanceLedger> ans = securityWorkPerformanceLedgerMapper.selectPage(page, queryWrapper);
        return ans.convert(u -> {
            SecurityWorkPerformanceLedgerVO v = new SecurityWorkPerformanceLedgerVO();
            BeanUtils.copyProperties(u, v);
            Organization organization = organizationMapper.selectById(u.getAssessedOrganizationId());
            v.setAssessedOrganizationName(organization != null ? organization.getName() : "该部门已被删除");
            SysUser user = sysUserMapper.selectById(u.getCreatorId());
            v.setCreatorName(user != null ? user.getName() : "该用户已被删除");
            return v;
        });
    }

    public SecurityWorkPerformanceLedgerVO getVOById(Long id) {
        SecurityWorkPerformanceLedger ledger = securityWorkPerformanceLedgerMapper.selectById(id);
        if (ledger == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        SecurityWorkPerformanceLedgerVO vo = new SecurityWorkPerformanceLedgerVO();
        BeanUtils.copyProperties(ledger, vo);
        Organization organization = organizationMapper.selectById(ledger.getAssessedOrganizationId());
        vo.setAssessedOrganizationName(organization == null ? " 该部门已被删除" : organization.getName());
        SysUser user = sysUserMapper.selectById(ledger.getCreatorId());
        vo.setCreatorName(user == null ? "该用户已被删除" : user.getName());
        return vo;
    }
}
